
/**
 * Copyright 2016 Yonyou Corporation Ltd. All Rights Reserved.
 * This software is published under the terms of the Yonyou Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 *
 * @Project Name : dms.manage
 * @File name : RoleMenuActionServiceImpl.java
 * @Author : yll
 * @Date : 2016年8月9日
 * <p>
 * ----------------------------------------------------------------------------------
 * Date       Who       Version     Comments
 * 1. 2016年8月9日    yll    1.0
 * <p>
 * <p>
 * <p>
 * <p>
 * ----------------------------------------------------------------------------------
 */

package com.yonyou.cyxdms.sysAuth.service.service.role;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.CommonConstants;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.constants.StringUtil;
import com.yonyou.cyxdms.sysAuth.service.entity.PermissionMenuActionPO;
import com.yonyou.cyxdms.sysAuth.service.mapper.MenuActionMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.PermissionMenuActionMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色菜单操作权限service接口实现类
 *
 * @author yll
 * @date 2016年8月9日
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleMenuActionServiceImpl implements RoleMenuActionService {


    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionMenuActionMapper permissionMenuActionMapper;

    @Autowired
    MenuActionMapper menuActionMapper;


    /**
     * 查询所有菜单操作按钮权限
     *
     * @author dengqiang
     * @since 2019/7/10
     */
    @Override
    public String[] findMenuAction(Integer permissionType, String permissionid) {
        List<String> list = new ArrayList<>();
        if (permissionid != null) {
            String[] ids = permissionid.split(",");
            for (String splId : ids) {
                list.add(splId);
            }
        }
        List<Map<String, Object>> listmap = roleMapper.findTreeMenuAction(list, permissionType);

        return StringUtil.getTreeMenuSome(listmap, "ACTION_CODE");
    }


    /**
     * 修改一条操作按钮权限
     *
     * @author dengqiang
     * @since 2019/7/12
     */
    @Override
    public void editOneAction(String oneAction, Integer permissionType,
                              String permissionId) {
        String[] arry = oneAction.split("-");
        String menuId = arry[0];

        List<Map<String, Object>> tmpermissionMenu = roleMapper.selectTmpermissionMenu(menuId, permissionId, permissionType);

        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);

        if (!CommonUtils.isNullOrEmpty(tmpermissionMenu)) {
            Object per = tmpermissionMenu.get(0).get("PERMISSION_MENU_ID");
            String s = per.toString();
            Long permissionMenuId = Long.valueOf(s);
            String checkids = arry[1];
            String[] checkid = checkids.split(",");
            permissionMenuActionMapper.insertAction(permissionMenuId, checkid, loginInfoDto.getUserId());
        }
    }


    /**
     * 修改一条操作按钮权限
     *
     * @author adu
     * @since 2019/9/26
     */
    @Override
    public void editOneActions(String oneAction, Integer permissionType, String permissionId) {
        String[] arry = oneAction.split("-");
        String menuId = arry[0];

        List<Map<String, Object>> tmpermissionMenu = roleMapper.selectTmpermissionMenu(menuId, permissionId, permissionType);

        if (!CommonUtils.isNullOrEmpty(tmpermissionMenu)) {
            Object per = tmpermissionMenu.get(0).get("PERMISSION_MENU_ID");
            String s = per.toString();
            Long permissionMenuId = Long.valueOf(s);
            String checkids = arry[1];
            String[] checkid = checkids.split(",");
            for (int i = 0; i < checkid.length; i++) {
                if (StringUtils.isNullOrEmpty(menuId)) {
                    throw new ServiceBizException("菜单代码为空");
                }
                if (StringUtils.isNullOrEmpty(checkid[i])) {
                    throw new ServiceBizException("操作按钮check为空");
                }
                LambdaQueryWrapper<PermissionMenuActionPO> queryWrapper = new QueryWrapper<PermissionMenuActionPO>().lambda();
                queryWrapper.eq(PermissionMenuActionPO::getPermissionMenuId, permissionMenuId);
                queryWrapper.eq(PermissionMenuActionPO::getMenuCuringId, Integer.parseInt(checkid[i].trim()));
                List<PermissionMenuActionPO> permissionmenuactionpos = permissionMenuActionMapper.selectList(queryWrapper);
                if (CommonUtils.isNullOrEmpty(permissionmenuactionpos)) {
                    PermissionMenuActionPO permissionmenuactionpo = new PermissionMenuActionPO();
                    permissionmenuactionpo.setIsEmpowered(ManageDictCodeConstants.STATUS_IS_YES);
                    permissionmenuactionpo.setPermissionMenuId(permissionMenuId);
                    permissionmenuactionpo.setMenuCuringId(Integer.parseInt(checkid[i].trim()));
                    permissionMenuActionMapper.insert(permissionmenuactionpo);
                } else {
                    permissionMenuActionMapper.modifyPermissionMenuAction(ManageDictCodeConstants.STATUS_IS_YES, permissionMenuId, Integer.parseInt(checkid[i].trim()));
                }

            }
        }
    }


    /**
     * 查询角色菜单操作按钮 复选框组
     *
     * @author adu
     * @since 2019/8/27
     */
    @Override
    public List<Map<String, Object>> remoteSoftUrl(Long menuId, Integer permissionType, String permissionids) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        List<Map<String, Object>> result = menuActionMapper.findListMenuAction(menuId, loginInfoDto.getAppId());
        if (!CommonUtils.isNullOrEmpty(result)) {
            for (int i = 0; i < result.size(); i++) {
                Map<String, Object> resultMap = result.get(i);
                Map<String, Object> map = new HashMap();
                map.put(CommonConstants.IS_FLEXIBLE, 10041002);
                map.put(CommonConstants.IS_EMPOWERED, 10041002);
                //如果是多个角色，则会有多条，然后将这多条合并
                String mc = resultMap.get("menuCuringId").toString();
                Integer menuCuringId = Integer.valueOf(mc);
                List<String> permissionidlist = new ArrayList<>();
                if (permissionids != null) {
                    String[] ids = permissionids.split(",");
                    for (String splId : ids) {
                        permissionidlist.add(splId);
                    }
                }
                List<Map<String, Object>> listmap = getListmap(menuId, permissionType, permissionidlist, menuCuringId);


                if (!CommonUtils.isNullOrEmpty(listmap)) {

                    //先查一下是否权限存在是否柔性为否，是否有权限为是的
                    List<Map<String, Object>> listmap3 = getListmap3(menuId, permissionType, permissionidlist, menuCuringId);
                    StringUtil.putMap(listmap3, map, listmap);
                }

                StringUtil.putResultMap(resultMap, map, "actionName");
            }
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> remoteUrl(Long menuId) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return menuActionMapper.findRemoteUrl(menuId, loginInfoDto.getAppId());

    }

    public List<Map<String, Object>> getListmap(Long menuId, Integer permissionType, List<String> permissionidlist, Integer menuCuringId) {
        List<Map<String, Object>> listmap = new ArrayList<>();
        if (StringUtils.isEquals(ManageDictCodeConstants.BILL_TYPE_PERMISSION_ROLE, permissionType.toString())) {//如果是角色
            listmap = menuActionMapper.findListmapOne(menuId, permissionidlist, menuCuringId);
        }
        if (StringUtils.isEquals(ManageDictCodeConstants.BILL_TYPE_PERMISSION_POSITION, permissionType.toString())) {//如果是职位
            listmap = menuActionMapper.findListmapTwo(menuId, permissionidlist, menuCuringId);
        }
        return listmap;
    }

    public List<Map<String, Object>> getListmap3(Long menuId, Integer permissionType, List<String> permissionidlist, Integer menuCuringId) {
        List<Map<String, Object>> listmap3 = new ArrayList<>();
        if (StringUtils.isEquals(ManageDictCodeConstants.BILL_TYPE_PERMISSION_ROLE, permissionType.toString())) {//如果是角色
            listmap3 = menuActionMapper.findListmap3One(menuId, permissionidlist, menuCuringId);
        }
        if (StringUtils.isEquals(ManageDictCodeConstants.BILL_TYPE_PERMISSION_POSITION, permissionType.toString())) {//如果是职位
            listmap3 = menuActionMapper.findListmap3Two(menuId, permissionidlist, menuCuringId);
        }
        return listmap3;
    }


}
